package com.example.zwlyz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zwlyz.models.CartItem;
import com.example.zwlyz.models.Product;
import com.example.zwlyz.R;

public class ProductDetailActivity extends AppCompatActivity {

    private TextView tvName;
    private TextView tvPrice;
    private TextView tvDescription;
    private ImageView ivImage;
    private Button btnAddToCart;
    private Button btnBuy;
    private ImageView ivCart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        tvName = findViewById(R.id.tvName);
        tvPrice = findViewById(R.id.tvPrice);
        tvDescription = findViewById(R.id.tvDescription);
        ivImage = findViewById(R.id.ivImage);
        btnAddToCart = findViewById(R.id.btnAddToCart);
        btnBuy = findViewById(R.id.btnBuy);
        ivCart = findViewById(R.id.ivCart);

        Product product = (Product) getIntent().getSerializableExtra("product");
        if (product != null) {

            tvName.setText(product.getName());
            tvPrice.setText(String.format("¥%.2f", product.getPrice()));
            tvDescription.setText(product.getDescription());
            ivImage.setImageResource(product.getImageResource());
        }

        btnAddToCart.setOnClickListener(v -> {
            if (product != null) {
                CartManager.addCartItem(new CartItem(

                        1,
                        1,
                        product.getId(),
                        1,
                        product.getName(),
                        product.getPrice()
                ));
                Toast.makeText(this, "已添加到购物车", Toast.LENGTH_SHORT).show();
            }
        });

        btnBuy.setOnClickListener(v -> {
            Toast.makeText(this, "购买成功", Toast.LENGTH_SHORT).show();
        });

        ivCart.setOnClickListener(v -> {
            startActivity(new Intent(this, CartActivity.class));
        });
    }
}